package com.amigo.online.provider.user.mapper;

import java.util.List;
import java.util.Map;

import com.amigo.online.provider.user.model.entity.InvitePeople;
import org.apache.ibatis.annotations.MapKey;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.apache.ibatis.type.LongTypeHandler;
import org.springframework.stereotype.Component;

import com.amigo.online.common.core.mybatis.MyMapper;
import com.amigo.online.common.param.common.KeyValueModel;
import com.amigo.online.common.param.dto.user.TipOffDto;
import com.amigo.online.common.param.result.UserAvatarInfo;
import com.amigo.online.common.param.result.UserInfo;
import com.amigo.online.common.param.result.VideoInfo.VideoUserInfo;
import com.amigo.online.provider.user.model.entity.User;
import com.amigo.online.provider.user.mongo.entity.UserEntity;

@Mapper
@Component
public interface UserMapper extends MyMapper<User> {

    /**
     * 根据手机号查询用户
     *
     * @param mobilephone
     * @return
     */
    User findByMobilephone(@Param("mobilephone") String mobilephone);

    /**
     * 根据帐号查询用户
     *
     * @param account
     * @return
     */
    User findByAccount(@Param("account") String account);

    /**
     * 根据facebookOpenId查询用户
     *
     * @param facebookOpenId
     * @return
     */
    User findByFacebookOpenId(@Param("facebookOpenId") String facebookOpenId);

    /**
     * 查询所有用户
     *
     * @param
     * @return
     */
    List<UserInfo> getAll();

    @MapKey("id")
    Map<Long, User> findMapByIdList(@Param("idList") List<Long> idList);

    List<User> findByIdList(@Param("idList") List<Long> idList);

    List<User> findModel(@Param("pageStart")  int pageStart,@Param("limit") int limit);


    Integer selectFriendsNum(Long id);

    Integer selectFollowNum(Long id);

    Integer selectFansNum(Long id);

    Integer selectGroupNum(Long id);

    int insertTipOff(TipOffDto tipOffDto);

    Integer getRegionId(@Param("phonetype") String phonetype, @Param("language") String language, @Param("regionname") String regionname);

    int getRightUserCountByIds(List<Long> ids);

    User findUserByToken(String token);

    List<Long> getfollowedByUserId(@Param("userId") Long userId, @Param("ids") List<Long> ids);

    Long selectUserIdById(Long targetId);

    String getUserCountryCodeById(Long id);

    Long checkTipOff(@Param("targetId") Long targetId);

    int updateAccount(User user);

    Long getNextId();

    //开启探探
    @Update("update user_tbl set openT = 1,open_time = #{openTime}  where id = #{id} ")
    int openT(User user);

    //关闭探探
    @Update("update user_tbl set openT = 0,close_time = #{closeTime} where id = #{id} ")
    int closeT(User user);

    @Select("select account, nickname, avatar from user_tbl where id = #{id}")
    VideoUserInfo getVideoUserInfoById(Long id);

    @Select("select u.id, u.nickname, u.sex, u.avatar, u.birthday, u.vip, u.celebrity, u.verified, u.sign, u.account,"
            + " p.checked, u.openT, p.photo as firstImg, u.avatar_small, u.job, u.secret from user_tbl u left JOIN user_photos_tbl p"
            + " on u.id = p.user_id where u.id >= #{startId} and u.id <= #{endId}")
    List<UserEntity> findAllUser(@Param("startId") Long startId, @Param("endId") Long endId);

    @Select("select id , avatar  from user_tbl where id >= #{startId} and id <= #{endId}")
    List<UserAvatarInfo> listUserBetweenStartIdAndEndId(@Param("startId") Long startId, @Param("endId") Long endId);

    @Results(
            {
                    @Result(column = "id", property = "id", typeHandler = LongTypeHandler.class),
                    @Result(column = "avatar", property = "value")
            }
    )
    @Select("select id, avatar from user_tbl where id >= #{startId} and id <= #{endId}")
    List<KeyValueModel<Long, String>> listUserBetweenStartIdAndEndId1(@Param("startId") Long startId, @Param("endId") Long endId);

    @Update("update user_tbl set avatar_small = #{small} where id = #{id}")
    void updateUserSmallAvatar(@Param("id") long key, @Param("small") String samllresult);

    @Select("select avatar as id, avatar_small as value from user_tbl where id = #{userId}")
    KeyValueModel<String, String> findUserSmallAvatarById(@Param("userId") Long userId);

    @Select("select avatar from user_tbl where id = #{userId}")
    String findUserAvatarById(@Param("userId") Long id);

    @Select("<script>"
            + "select id,nickname,avatar,avatar_small "
            + "from user_tbl "
            + "where "
            + "nickname = #{param} "
            + "or mobilephone = #{param} "
            + "or account = #{param} "
            + "</script>")
    List<User> getUserByNicknameOrPhone(String param);

    @Select("select id from merchant_tbl where user_id = #{userId} and status = 2 limit 1")
    Long checkMerchant(@Param("userId") Long userId);

    @Select("select id,nickname,mobilephone from user_tbl d where d.id in (select user_id from invite_code_tbl where code in (select code from (select code,count(code) num from invite_code_record_tbl GROUP BY code ) a where num >100))")
    List<InvitePeople> selectInviteNum();
}
